package com.fingersoft.theme

import android.graphics.Color
import android.view.View
import cn.fingersoft.util.BuildConfigUtil.getString
import com.fingersoft.common.preference.UserAppPreferenceHelper
import com.fingersoft.common.preference.provider.UserIdProvider
import com.fingersoft.im.R
import com.fingersoft.im.ui.home.useUi3
import com.fingersoft.im.utils.AppUtils
import com.fingersoft.image.ImageUtils
import java.util.*

/**
 * @author wangxf
 * @date 2018/1/31
 */
object ThemeM {
    private val defaultThemeColor = getString("defaultThemeColor", if (useUi3()) "ui3_blue" else "").trim { it <= ' ' }

    @JvmStatic
    fun saveThemeColor(colors: Array<String?>) {
        userAppPreferenceHelper.putStringSet(ThemeConstant.THEME_COLOR, HashSet(Arrays.asList<String>(*colors)))
    }

    private val userAppPreferenceHelper: UserAppPreferenceHelper = UserAppPreferenceHelper(object : UserIdProvider() {
        override fun getUserId(): String {
            return AppUtils.getUser().userId!!
        }
    })

    private fun getDefaultThemeColor(): Set<String> {
        return HashSet()
    }

    private val themeColorList: List<String>
        get() {
            val colorSet = userAppPreferenceHelper.getStringSet(ThemeConstant.THEME_COLOR, getDefaultThemeColor())
            return ArrayList(colorSet)
        }

    private fun getTheme(color: String?): Int {
        var colorName = color
        if (color != null && color.contains("-")) {
            colorName = color.substring(0, color.indexOf("-"))
        }
        if ("blue" == colorName) {
            return R.style.AppThemeNoActionBar_blue
        } else if ("orange" == colorName) {
            return R.style.AppThemeNoActionBar_orange
        } else if ("red" == colorName) {
            return R.style.AppThemeNoActionBar_red
        } else if ("green" == colorName) {
            return R.style.AppThemeNoActionBar_green
        } else if ("purple" == colorName) {
            return R.style.AppThemeNoActionBar_purple
        }
        return R.style.AppThemeNoActionBar
    }

    /**
     * 获取主题
     */
    @JvmStatic
    val theme: Int
        get() {
            val style = userAppPreferenceHelper.getString("theme_style", defaultThemeColor)
            return getTheme(style)
        }

    /* 解析xml中配置的颜色名称 */
    @JvmStatic
    fun getColorByName(color: String?): String {
        val colorList = themeColorList
        var colorName: String? = null
        for (colorValue in colorList) {
            if (colorValue.contains(color!!)) {
                colorName = colorValue.substring(colorValue.indexOf("-") + 1)
                break
            }
        }
        if (colorName == null) {
            colorName = colorList[0].substring(colorList[0].indexOf("-") + 1)
        }
        return colorName
    }

    /* 获取主题颜色值 */
    @JvmStatic
    val themeColor: Int
        get() {
            val style = userAppPreferenceHelper.getString("theme_style", defaultThemeColor)
            return Color.parseColor(getColorByName(style))
        }

    /* 获取主题颜色的名称类型 */
    val themeColorType: String
        get() = userAppPreferenceHelper.getString("theme_style", defaultThemeColor) ?: defaultThemeColor

    /* 设置view的背景色 */
    @JvmStatic
    fun setThemeStyle(view: View?) {
        if (AppUtils.useTheme()) {
            view?.background = ImageUtils.getImageBackground(16, themeColor)
        }
    }
}
